package com.platform.server.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paltform.server.entity.SysUser;
import com.platform.server.dao.SystemUserDao;
import com.platform.server.service.SystemUserService;
import com.platform.server.util.RegexUtil;
import com.szrca.ima.utils.StringTools;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/sysUser")
public class SystemUserController {
	
	@Autowired
	private SystemUserService systemUserService;
	
	@Autowired
	private SystemUserDao systemUserDao;

	
	/**
	 * 用户登录
	 */
	@ResponseBody
	@RequestMapping("/login")
	public void getList(HttpServletRequest request,String username,String password,HttpServletResponse response,
			HttpSession session) throws Exception{
		Map<String,Object> map = new HashMap<>();
		JSONObject jsonObject = new JSONObject();
		if(StringTools.isEmpty(username)){
			jsonObject.put("msg", "请输入用户名");
		}
		if(StringTools.isEmpty(password)){
			jsonObject.put("msg", "请输入用户密码");
		}
		map.put("sysPhone", username);
		List<Map<String,Object>> sysUserList = systemUserService.userLogin(map);
		if(sysUserList.size()>0){
			Integer status = (Integer) sysUserList.get(0).get("status");
			if(status == 2){
				jsonObject.put("msg", "账号已被禁用");
			}else{
				map.put("sysPassword", password);
				List<Map<String,Object>> sysUserList1= systemUserService.userLogin(map);
				if(sysUserList1.size()>0){
					map.put("role_id", sysUserList1.get(0).get("sys_role"));
					Map<String,Object> m=sysUserList1.get(0);
					JSONObject jo = JSONObject.fromObject(m);
					SysUser su = (SysUser) JSONObject.toBean(jo,SysUser.class);
					List<Map<String,Object>> sysRoleList = systemUserService.sysRoleList(map);
					System.out.println(sysRoleList.get(0).get("action_id").toString());
					session.setAttribute("sr",sysRoleList.get(0).get("action_id").toString() );
					session.setAttribute("user", su);
					jsonObject.put("msg", "成功");
				}else{
					jsonObject.put("msg", "密码不正确，请输入正确的密码");
				}
			}
		}else{
			jsonObject.put("msg", "账号不存在");
		}
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	
	/**
	 * 用户退出登陆
	 */
	@RequestMapping("/loginout")
	@ResponseBody
	 public void loginout(HttpServletRequest request,HttpServletResponse response) {
	    HttpSession session = request.getSession();
	    session.removeAttribute("user");
	    session.removeAttribute("sr");
	    try {
	    	//response.sendRedirect(request.getContextPath()+"/index.jsp");
	    	response.getWriter().write("<script>window.top.location.href=\"../index.jsp\";</script>");
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	  }
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/sysRoleList")
	public Object sysRoleList(HttpServletRequest request,String startTime,String endTime,Integer status,String roleName){
		Map<String,Object> map = new HashMap<>();
		if(!StringTools.isEmpty(startTime)&&!StringTools.isEmpty(endTime)){
			map.put("startTime", startTime);
			map.put("endTime", endTime);
		}
		if(!RegexUtil.isNull(status)){
			map.put("status", status);
		}
		if(!StringTools.isEmpty(roleName)){
			map.put("roleName", roleName);
		}
		List<Map<String,Object>> sysRoleList = systemUserService.sysRoleList(map);
		SimpleDateFormat fdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(int i = 0;i < sysRoleList.size();i++){
			sysRoleList.get(i).put("createTime",fdt.format(sysRoleList.get(i).get("createTime"))); 
			Integer status1 =(Integer) sysRoleList.get(i).get("status");
			String st = status1 == 1? "启用":"禁用";
			sysRoleList.get(i).put("status",st); 
		}
		request.setAttribute("sysRoleList",sysRoleList);
		request.setAttribute("nums",sysRoleList.size());
		return "/ui/role/roleManager.jsp";
	}
	
	/**
	 * 添加角色
	 */
	@RequestMapping("/insertSysRole")
	public void insertSysRole(String role_name,String action_id,String action_name,String status,
			HttpServletResponse response) throws Exception{
		int flag = systemUserService.insertSysRole(role_name,action_id,action_name,status);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	
	/**
	 * 查看角色详细信息
	 */
	@RequestMapping("/selectSysRoleInfo")
	public Object selectSysRoleInfo(String role_id,HttpServletRequest request) throws Exception{
		List<Map<String,Object>> roles = systemUserService.selectSysRoleInfo(Integer.parseInt(role_id));
		String str = (Integer)roles.get(0).get("status") == 1? "启用" : "禁用";
		roles.get(0).put("status", str);
		request.setAttribute("roles",roles);
		System.out.println(roles.get(0).get("action_id"));
		return "/ui/role/update_sysRole.jsp";
	}
	
	/**
	 * 修改角色信息
	 */
	@RequestMapping("/updateSysRole")
	public void updateSysRole(String role_id,String role_name,String action_id,String action_name,
			String status, HttpServletResponse response) throws Exception{
		int flag = systemUserService.updateSysRole(Integer.parseInt(role_id),role_name,action_id,action_name,status);
		if("2".equals(status)){
			//修改账号信息
			systemUserDao.updateSystemUser(Integer.parseInt(role_id));
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	
	/**
	 * 删除角色
	 */
	@RequestMapping("/deleteRole")
	public Object deleteRole(String id){
		int flag = systemUserService.deleteRole(id);
		return "/sysUser/sysRoleList";
	}
	
	/**
	 * 账号列表
	 */
	@RequestMapping("/sysUserList")
	public Object getList(HttpServletRequest request,String startTime,String endTime,Integer status,String sysName){
		Map<String,Object> map = new HashMap<>();
		SimpleDateFormat fdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(!StringTools.isEmpty(startTime)&&!StringTools.isEmpty(endTime)){
			map.put("startTime", startTime);
			map.put("endTime", endTime);
		}
		if(!RegexUtil.isNull(status)){
			map.put("status", status);
		}
		if(!StringTools.isEmpty(sysName)){
			map.put("sysName", sysName);
		}
		List<Map<String,Object>> sysUserList = systemUserService.getSysUserList(map);
		for(int i = 0;i < sysUserList.size();i++){
			sysUserList.get(i).put("createTime",fdt.format(sysUserList.get(i).get("createTime"))); 
			Integer status1 =(Integer) sysUserList.get(i).get("status");
			String st=status1==1?"启用":"禁用";
			sysUserList.get(i).put("status",st); 
		}
		request.setAttribute("sysUserList",sysUserList);
		request.setAttribute("nums",sysUserList.size());
		return "/ui/role/sysUserList.jsp";
	}
	
	/**
	 * 添加账号之前查看角色列表
	 */
	@RequestMapping("/addSysUser")
	public Object addSysUser(String sys_id,HttpServletRequest request) throws Exception{
		Map<String,Object> map = new HashMap<>();
		List<Map<String,Object>> roles = systemUserService.sysRoleInfoList(map);
		request.setAttribute("roles",roles);
		return "/ui/role/add_sysUser.jsp";
	}
	
	/**
	 * 添加账号
	 */
	@RequestMapping("/insertSysUser")
	public void insertSysUser(String sys_phone,String sys_name,String sys_password,String sys_role,
			String sys_role_name,String company_name,String status,HttpServletResponse response) throws Exception{
		Map<String,Object> map = new HashMap<>();
		map.put("sysPhone", sys_phone.trim());
		int flag =0;
		List<Map<String,Object>> sysUserList = systemUserService.getSysUserList(map);
		if(sysUserList.size()>0){
			flag = 3;//账号已存在
		}else{
			List<Map<String,Object>> roles = systemUserService.selectSysRoleInfo(Integer.parseInt(sys_role));
			if(roles.size()>0){
				sys_role_name = (String) roles.get(0).get("role_name");
			}
			flag = systemUserService.insertSysUser(sys_phone.trim(),sys_name,sys_password,sys_role,sys_role_name,company_name,status);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	
	/**
	 * 删除账号
	 */
	@RequestMapping("/deleteSysUser")
	public Object deleteSysUser(String id){
		int flag = systemUserService.deleteSysUser(id);
		return "/sysUser/sysUserList";
	}
	
	/**
	 * 查看用户详情
	 */
	@RequestMapping("/selectSysUserInfo")
	public Object selectSysUserInfo(String sys_id,HttpServletRequest request) throws Exception{
		Map<String,Object> map = new HashMap<>();
		List<Map<String,Object>> lists = systemUserService.selectSysUserInfo(Long.parseLong(sys_id));
		List<Map<String,Object>> roles = systemUserService.sysRoleInfoList(map);
		request.setAttribute("lists",lists);
		request.setAttribute("roles",roles);
		return "/ui/role/update_sysUser.jsp";
	}
	
	/**
	 * 编辑账号信息
	 */
	@RequestMapping("/updateSysUser")
	public void updateSysUser(String sys_phone,String sys_name,String sys_password,String sys_role,
			String sys_role_name,String company_name,String status,String sys_id,
			HttpServletResponse response) throws Exception{
		Map<String,Object> map = new HashMap<>();
		map.put("sysPhone", sys_phone);
		int flag =0;
		List<Map<String,Object>> sysUserList = systemUserService.getSysUserList(map);
		/*if(sysUserList.size()>0){
			flag =3;//账号已存在
		}else{*/
			List<Map<String,Object>> roles = systemUserService.selectSysRoleInfo(Integer.parseInt(sys_role));
			if(roles.size()>0){
				sys_role_name = (String) roles.get(0).get("role_name");
			}
	//}
		flag = systemUserService.updateSysUser(sys_phone,sys_name,sys_password,sys_role,sys_role_name,company_name,status,Integer.parseInt(sys_id));
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
}
